<#import "_layout.html" as layout>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${(article.title)!}-法律咨询-律栗</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="baidu-site-verification" content="codeva-mcy1QWF2qT" />
<meta name="keywords" content="${keywords!''}">
<meta name="description" content="${article.summary}">
<link href="${ctx()}/amaze/css/amazeui.min.css" rel="stylesheet">
<link href="${ctx()}/css/base.css" rel="stylesheet">
<link href="${ctx()}/css/m.css" rel="stylesheet">
<link href="${ctx()}/css/swiper.min.css" rel="stylesheet">
<link href="${ctx()}/css/jquery.fancybox.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,700,800' rel='stylesheet' type='text/css'>
<script src="${ctx()}/js/jquery-3.6.0.min.js"></script>
<script src="${ctx()}/amaze/js/amazeui.min.js"></script>
<@layout.stat/>
</head>

<@layout.body>
<main>
<!--  <div class="weizhi">您现在的位置是：<a href="index.html">首页</a>&nbsp;>&nbsp;<a href="article_list.html">图文列表</a></div>-->
  <div class="lbox">
    <article>
      <h1 class="ar_title">${(article.title)!}</h1>
      <div class="ar_info"><span><@formatTime value=(article.created)!/></span><span>阅读 ${(article.viewCount)!}</span></div>
      <div class="content">
        ${(article.contentHtml)!}
      </div>
      <#if (article.attachId)??>
        <div class="diggit">
          <a id="download-btn" href="javascript:void(0)">我要下载<b id="diggnum">(4)</b></a>
        </div>
        <div id="payQrcodeDiv" class="diggit">
          <a href="javascript:void(0)">请扫码支付</a>
          <div id="output"></div>
        </div>
      </#if>
<!--      <div class="tags_list"><a href="article_list.html" target="_blank">成长</a></div>-->
<!--      <div class="pinglun">
        <p style="color:#f00">注意：静态页面不含评论插件</p>
      </div>-->
      <div class="info-pre-next">
        <ul>
          <@prevArticleTag articleId=(article.id)!>
            <#if data??>
          <li><a href="${data.url!}">
            <h3>${data.title!}</h3>
            <p><span><img src="${data.thumbnail!}" alt=""> 上一篇</span><span><@formatTime value=(data.created)! format="yyyy-MM-dd"/></span></p>
          </a></li>
            </#if>
          </@prevArticleTag>

            <@nextArticleTag articleId=(article.id)!>
                <#if data??>
                    <li>
                      <a href="${data.url!}">
                        <h3>${data.title!}</h3>
                        <p><span><@formatTime value=(data.created)! format="yyyy-MM-dd"/></span><span>下一篇 <img src="${data.thumbnail!}" alt=""></span></p>
                      </a></li>
                </#if>
            </@nextArticleTag>

        </ul>
      </div>
    </article>
    <div class="other">
      <@relatedArticleList articleId=(article.id)! count=10>
        <h4 class="btitle">为你推荐</h4>
        <ul class="blogs_list">
        <#if data??>
          <#list data as item>
          <li>
            <section>
              <h2><a href="${(item.url)!}" target="_blank">${(item.title)!}</a></h2>
              <p>${(item.summary)!}</p>
              <div class="blogs_info"><span class="timer"><@formatTime value=(item.created)! format="yyyy-MM-dd"/></span> <span class="view">${(item.viewCount)!}</span></div>
            </section>
          </li>
          </#list>
        </#if>
        </ul>
      </@relatedArticleList>

    </div>
  </div>
  <aside class="rbox">
    <div class="hot_news">
      <div class="side_title">推荐问答</div>
      <ul class="pic_news">

        <@articleListTag tagId=1 orderBy="created">
          <#if data??>
            <#list data as item>
              <li><a href="${(item.url)!}" target="_blank">
                <h3>${(item.title)!}</h3>
                <span><@formatTime value=(item.created)! format="yyyy-MM-dd"/></span></a></li>
            </#list>
          </#if>
        </@articleListTag>

      </ul>

    </div>
    <div class="tj_news">
      <div class="side_title">热门问答</div>
      <ul class="pic_news">
        <@articleListTag tagId=2 orderBy="created">
            <#if data??>
              <#list data as item>
                <li><a href="${(item.url)!}" target="_blank">
                  <h3>${(item.title)!}</h3>
                  <span><@formatTime value=(item.created)! format="yyyy-MM-dd"/></span></a></li>
              </#list>
            </#if>
        </@articleListTag>
      </ul>
    </div>

 <!--    <#if article.tagList?? && (article.tagList?size>0)>
      <div class="tags">
        <div class="side_title">标签</div>
        <ul>
          <#list article.tagList as item>
          <li><a href="${item.url}">${item.tagName}</a></li>
          </#list>
        </ul>
      </div>
    </#if> -->

  </aside>
</main>

<script id="commentItemTemplate" type="text/html">
  {{each records as item i}}
  <li>
    <div class="topic-comment-box">
      <div class="comment-header">
        <div class="author-img">
          {{if item.authorAvatar != null}}
          <img src="{{item.authorAvatar}}" alt="">
          {{else}}
          <img src="/header.jpg" alt="">
          {{/if}}
        </div>
        <div class="author-name">
          <h5>{{item.author}}<span>- {{item.created}}</span></h5>
        </div>
      </div>
      <div class="comment-body">
        <p>{{item.content}}</p>
      </div>
      <div class="comment-footer">
        <#--<ul class="comment-footer-icon">
        <li><a href="#"><i class='bx bx-like'></i></a></li>
        <li><a href="#"><i class='bx bx-heart'></i></a></li>
      </ul>-->
        <#--<button>Reply</button>-->
      </div>
    </div>
  </li>
  {{/each}}
</script>

</@layout.body>
<@layout.script>
<script src="${ctx()}/js/template-web.js"></script>
<script src="${ctx()}/js/pagination.js"></script>
<script src="${ctx()}/js/jquery.qrcode.min.js"></script>
<script>

  var orderId;
  /**
   * 创建订单
   */
  function createOrder() {
    if(orderId != null) {
      payOrder(orderId)
      return;
    }

    var products = new Array();
    var product = {
      id: ${(article.id)!},
      type: '',
      sku: '',
      num: 1
    }
    products.push(product);
    var orderParams = {
      products: products,
      buyerMsg: "",
      priceTypeClass: "com.fastcms.plugin.pay.download.PayDownloadPriceService"
    };

    ajaxJsonPost("/fastcms/api/client/order/save", JSON.stringify(orderParams), function (result) {
      orderId = result.data;
      payOrder(orderId);
    });
  }

  var myTimer = null;
  /**
   * 支付订单
   */
  function payOrder(orderId) {
    ajaxGet("/fastcms/api/client/pay/wxPay/NATIVE/getQrPay?orderId=" + orderId, function (result) {
      $('#output').empty();
      $('#output').qrcode({ width: 150, height: 150, text: result });
      showPayWin();
      myTimer = setInterval(function () {
        checkOrderPayStatus(orderId);
      }, 2000)
    })
  }

  /**
   * 检查订单支付状态
   * @param orderId
   */
  function checkOrderPayStatus(orderId) {
    ajaxGet("/fastcms/api/client/order/status/check/"+orderId, function () {
      hidePayWin();
      if(myTimer != null) {
        clearInterval(myTimer);
      }
    }, function(){})
  }

  function getComment(pageNum) {
    ajaxGetWithoutLogin("/fastcms/api/client/article/comment/list/${(article.id)!}?pageNum="+pageNum, function(result) {
      $("#all-comments").empty();
      var html = template('commentItemTemplate', result.data);
      $("#all-comments").append(html);

      const pageSize = result.data.size;  // 默认页码大小
      const dataCount = result.data.total;
      const dataPages = result.data.pages;
      new Pagination('#comment-page', {
        pageSize: pageSize,
        autoLoad: true,
        unit: '条',
        toPage: function(index, _pageSize) {

          if(dataCount <=0) return;
          // 设置记录总数，用于生成分页HTML内容
          if (index === 0 || _pageSize) this.updateCount(dataCount, _pageSize)

          // 根据页码以及分页大小生成html内容
          let pageListHtml = '<li><a href="javascript:getComment(1)"><i class="bi bi-arrow-left"></i></a></li>'
          for (var i = 1; i <= dataPages; i++) {
            pageListHtml += '<li><a href="javascript:getComment('+i+')">'+ i +'</a></li>'
          }
          pageListHtml += '<li><a href="javascript:getComment('+dataPages+')"><i class="bi bi-arrow-right"></i></a></li>';
          $('#comment-page').html(pageListHtml)
        }
      })

    }, false);
  }

  $(function () {

    getComment(1);

    submitForm("myForm", {
      context: { required: true }
    },{
      context: { required: "请输入评论内容" }
    }, function(result) {
      if(result.code == 200) {
        alert("评论成功");
        getComment(1);
        $("#myForm")[0].reset();
      } else {
        alert("评论失败");
      }
    })

    $("#download-btn").click(function () {
      var fileUrl = '/fastcms/plugin/pay/download/${(article.id)!}?accessToken=' + getToken();
      ajaxGet(fileUrl, function () {
        $("<iframe>").hide().prop("src", fileUrl).appendTo("body");
      }, function (result) {
        if(result.code == 100500) {
          createOrder();
        } else {
          alert(result.message);
        }
      })
    });

    hidePayWin();

  });
</script>
</@layout.script>